package cn.xspace.wnx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpManager;
import cn.xspace.wnx.R;
import cn.xspace.wnx.adapter.ServiceObjSearchAdapter;
import cn.xspace.wnx.entity.ServiceObjBean;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ServiceObjSearchActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private EditText searchEdit;
    private TextView cancelText;
    private LinearLayout topLinear;
    private LayoutInflater layoutInflater;
    private View resultView;
    private List<ServiceObjBean> list;
    private ServiceObjSearchAdapter adapter;
    private String name;
    private String searchKey = "";
    private String searchType = "";
    private String user_id;
    private String serviceobj_id;
    private ServiceObjBean serviceObjBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        //super.initView();
        setContentView(R.layout.activity_service_obj_search);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        searchEdit = (EditText) findViewById(R.id.edit_servicesearch);
        cancelText = (TextView) findViewById(R.id.txt_cancel);
        //        topLinear = (LinearLayout) findViewById(R.id.linear_top);
        //        layoutInflater = LayoutInflater.from(mContext);
        //        resultView = layoutInflater.inflate(R.layout.service_obj_search_item, null);
        mListView = (ListView) findViewById(R.id.listview_service_obj_item);
        //                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams
        // .MATCH_PARENT,
        //                        LinearLayout.LayoutParams.WRAP_CONTENT);
        //                params.setMargins(0, 20, 0, 0);
        //                resultView.setLayoutParams(params);

    }


    @Override
    protected void initEvent() {
        super.initEvent();
        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //                if(TextUtils.isEmpty(searchEdit.getText().toString())){
                //                    mListView.setAdapter(null);
                //                }


            }

            @Override
            public void afterTextChanged(Editable editable) {
                //topLinear.removeView(resultView);
                String key = editable.toString();

                if (!TextUtils.isEmpty(key)) {
                    requestData();
                }
                if (TextUtils.isEmpty(key)) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                }


            }
        });
        cancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView.setOnItemClickListener(this);
    }

    private void requestData() {
        name = searchEdit.getText().toString();
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id", MyInfoUtil.getMyUserid(mContext));
        map.put("plat_code", MyInfoUtil.getPlatCode());
        map.put("search_name", name);
        registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.QUERY_SERVICEOBJ, map)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseReturnBean, List<ServiceObjBean>>() {
                    @Override
                    public List<ServiceObjBean> call(BaseReturnBean baseReturnBean) {
                        List<ServiceObjBean> dataList = new ArrayList<>();
                        Gson mGson = new Gson();
                        dataList = mGson.fromJson(baseReturnBean.data, new
                                TypeToken<List<ServiceObjBean>>() {
                                }.getType());
                        return dataList;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<ServiceObjBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<ServiceObjBean> dataList) {
                        list.clear();
                        LogUtil.d("CCCCC", dataList.toString());
                        //                        hideWaitDialog();
                        if (dataList.size() > 0) {
                            list.addAll(dataList);
                            Log.d("VVVV", list.toString());
                            //topLinear.addView(resultView);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }));
    }

    @Override
    protected void initData() {
        //super.initData();
        list = new ArrayList<>();
        adapter = new ServiceObjSearchAdapter(mContext, list);
        mListView.setAdapter(adapter);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        final ServiceObjBean bean = list.get(i);
        Intent intent = new Intent(ServiceObjSearchActivity.this, MyUserInfoActivity.class);
        user_id = bean.user_id;
        serviceobj_id = bean.serviceobj_id;
        intent.putExtra("friend_id", user_id);
        intent.putExtra("serviceobj_id", serviceobj_id);
        startActivity(intent);
    }
}

